package com.zzpcct.wh.work.mycaipu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zzpcct.wh.work.mycaipu.adapter.MRvAdapter;
import com.zzpcct.wh.work.mycaipu.databinding.ActivityMainBinding;
import com.zzpcct.wh.work.mycaipu.entity.RecipeMode2;
import com.zzpcct.wh.work.mycaipu.entity.ResultsBean;
import com.zzpcct.wh.work.mycaipu.service.MyAppService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    MyAppService myAppService = retrofit.create(MyAppService.class);
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        //binding.rv

        getAllRecipe();

    }

    private void getAllRecipe() {
        Call<RecipeMode2> call = myAppService.getRecipte(10);
        call.enqueue(new Callback<RecipeMode2>() {
            @Override
            public void onResponse(Call<RecipeMode2> call, Response<RecipeMode2> response) {
                List<ResultsBean> results = response.body().getResults();
                for (ResultsBean r:
                        results)
                      {
                          r.save();
                }
                MRvAdapter adapter=new MRvAdapter(MainActivity.this);
                binding.rv.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<RecipeMode2> call, Throwable t) {
                Log.e(TAG, "onFailure: ", t);
            }
        });
    }
}
